package com.example.defaultshop;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class CustomerActivity extends Activity {
	private SharedPreferences preferences;
	private EditText name, phone;
	private Context context;
	private Button ok;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_customer);
		
		this.context = this;
		preferences = context.getSharedPreferences("DefaultShop", MODE_PRIVATE);
		
		name = (EditText) findViewById(R.id.txtCustomerName);
		name.setText(preferences.getString("name", ""));
		
		phone = (EditText) findViewById(R.id.txtCustomerPhone);
		phone.setText(preferences.getString("phone", ""));
		
		ok = (Button) findViewById(R.id.btnCustomerDone);
		ok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				String n = name.getText().toString();
				String p = phone.getText().toString();
				
				if (p.length() != 8) {
					Toast.makeText(context, context.getResources().getString(R.string.error_checkout_cellphone_digits), Toast.LENGTH_LONG).show();
				} else if (n.length() > 0 && p.length() > 0) {
					Editor editor = preferences.edit();
					editor.putString("name", n);
					editor.putString("phone", p);
					editor.commit();
					finish();
				} else
					Toast.makeText(context, context.getResources().getString(R.string.error_customer_info), Toast.LENGTH_LONG).show();
				}
			});
	}
	
	@Override
	public void onBackPressed() { }
}
